package com.dkhseep.bigevent.service.impl;

import com.dkhseep.bigevent.mapper.ArticleMapper;
import com.dkhseep.bigevent.pojo.Article;
import com.dkhseep.bigevent.pojo.PageBean;
import com.dkhseep.bigevent.service.ArticleService;
import com.dkhseep.bigevent.util.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 功能:
 * 作者:Dksheep
 * 日期:2024/10/1010:17
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Override
    public void addArticle(Article article) {
        Map<String,Object>cliams = ThreadLocalUtil.get();
        Integer id = (Integer) cliams.get("id");
        article.setCreateUser(id);
        articleMapper.addArticle(article);

    }

    @Override
    public PageBean<Article> getlist(Integer pageNum, Integer pageSize, Integer categoryId, String state) {
        PageBean<Article> pageBean = new PageBean<>();

        //开启分页查询  需导入pagehelper依赖
        PageHelper.startPage(pageNum, pageSize);

        Map<String,Object>cliams = ThreadLocalUtil.get();
        Integer userId = (Integer) cliams.get("id");

        List<Article>articleList = articleMapper.getlist(categoryId,state,userId);
        articleList = (Page<Article>) articleList; //转成Page，Page有提供getTotal()、getResult()
        pageBean.setTotal(((Page<Article>) articleList).getTotal());
        pageBean.setItems(((Page<Article>) articleList).getResult());
        return pageBean;

    }
}
